<script type="text/javascript" src="__JS__/template.min.js" charset="utf-8"></script>
<script type="text/javascript" src="__JS__/pintuer.js" charset="utf-8"></script>
<div class="admin">
    <div class="panel admin-panel">
        <div class="panel-head"><strong>限时折扣详情</strong></div>
        <table class="table table-hover">
            <tbody>
                <tr>
                    <td class="w90 tr"><strong>活动名称：</strong></td>
                    <td class="w120 tl">{$re['xianshi_name']}</td>
                    <td class="w90 tr"><strong>开始时间：</strong></td>
                    <td class="w120 tl">{$re['start_time']|date='Y-m-d',###}</td>
                    <td class="w90 tr"><strong>结束时间：</strong></td>
                    <td class="w120 tl">{$re['end_time']|date='Y-m-d',###}</td>
                    <td class="w90 tr"><strong>购买下限：</strong></td>
                    <td class="w120 tl">{$re['lower_limit']}</td>
                    <td class="w90 tr"><strong>状态：</strong></td>
                    <td class="w120 tl">正常</td>
                </tr>
            </tbody>
        </table>
        <div class="padding border-bottom">
            <!--<div class="bg-back radius padding">
                <h4>说明：</h4>
                <p class="text-indent">满即送活动包括店铺所有商品，活动时间不能和已有活动重叠</p>
                <p class="text-indent">每个满即送活动最多可以设置3个价格级别，点击新增级别按钮可以增加新的级别，价格级别应该由低到高</p>
            </div>-->
        </div>
    </div>
    <div class="line30"></div>
    <div class="panel admin-panel">
        <div class="panel-head"><strong>商店产品列表</strong></div>

        <div id="div_goods_search_result" class="line-middle margin-left margin-top">

            <foreach item='v' name='list'>
                <div class="xl12 xs6 xm4 xb1 padding-small-top  margin-bottom margin-right border border-dashed">
                    <div class="media padding-bottom clearfix">
                        <a href="#">
                            <img src="{$v['goods_thumb']}!100x100" class="radius img-responsive text-center" alt="...">
                        </a>
                        <div class="media-body">
                            <strong>{$v['goods_title']|sub_str=###,0,7}</strong>
                            <a>本店售价：{$v['goods_price']}元</a>
                            <button class="button margin-top bg-main dialogs" id="myGoods" data-toggle="click" data-target="#mydialog" data-mask="1" data-goods="{$v['goods_id']}" data-width="20%">选择此商品</button>
                        </div>
                    </div>
                </div>
            </foreach>

        </div>

        <div class="panel-foot text-center">
            {$page}
        </div>
    </div>
    <div class="line30"></div>
    <div class="panel admin-panel">
        <div class="panel-head"><strong>活动选购的产品</strong></div>
        <table class="table table-hover">
            <thead>
                <tr>
                    <th class="w10"></th>
                    <th class="w50">商品图</th>
                    <th class="tl">商品名称</th>
                    <th class="w90">商品价格</th>
                    <th class="w120">折扣价格</th>
                    <th class="w120">折扣率</th>
                    <th class="w120">操作</th>
                </tr>
            </thead>
            <tbody id="xianshi_goods_list">
                <tr id="xianshi_goods_list_norecord" style="display:none">
                    <td class="norecord" colspan="20"><div class="warning-option"><i class="icon-warning-sign"></i><span>暂无符合条件的数据记录</span></div></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

<div id="mydialog">
    <div class="dialog">
        <div class="dialog-head">
            <span class="dialog-close close rotate-hover"></span><strong>添加商品</strong>
        </div>
        <div class="dialog-body">
            <ul class="list-media">
                <li>
                    <div class="media media-x">
                        <a class="float-left" href="#" id="pics">
                            <img src="" class="radius" alt="...">
                        </a>
                        <div class="media-body">
                            <strong id="title"></strong>
                            <span style="width: 100%;float: left;line-height: 42px;">销售价格：¥<b id="price">0.00</b></span>
                            <span style="width: 100%;">折扣价格：<input name="dialog_xianshi_price" id="dialog_xianshi_price" class="inputL input_smallL" value="" type="text" /><em class="add-on">元</em></span>
                            <div id="dialog_add_xianshi_goods_error" style="display: none;">信息错误！</div>
                            <input id="dialog_goods_id" type="hidden" value="0">
                            <input id="dialog_input_goods_price" type="hidden" value="0.00">
                        </div>
                    </div>
                </li>
            </ul>
        </div>
        <div class="dialog-foot">
            <button class="dialog-close button">取消</button>
            <a id="btn_add_xianshi_goods_submit" class="button bg-green" href="javascript:void(0);">提交</a>
        </div>
    </div>
</div>


<script type="text/javascript">
    var temp = $("#mydialog").html();
        // 当前编辑对象，默认为空
        $edit_item = {};
    $(document).ready(function () {

        $('.media-body').on('click', '#myGoods', function () {
            var g = $(this).data('goods');
            $.post("{:U('getOneGoods')}",
                    {goods_id: g},
                    function (data) {
                        if (data.result) {
                            $('#price').html(data.price);
                            $('#dialog_input_goods_price').val(data.price);
                            $('#title').html(data.title);
                            $('#dialog_goods_id').val(data.gid);
                            $('#pics img').attr('src', data.pics);
                        } else {
                            layer.msg(data.message);
                        }
                    },
                    'json');
        });
        
        $('#xianshi_goods_list').on('click', '.dialogs', function () {
            var g = $(this).attr('data-xianshi-goods-id');
            $edit_item = $(this).parents('tr.bd-line');
            
            $.post("{:U('getOneGood')}",
                    {goods_id: g},
                    function (data) {
                        if (true) {
                            $('#price').html(data.price);
                            $('#dialog_input_goods_price').val(data.price);
                            $('#title').html(data.title);
                            $('#dialog_goods_id').val(data.gid);
                            $('#dialog_xianshi_price').val(data.fprice);
                            $('#pics img').attr('src', data.pics);
                            $('.dialog-foot a').attr('id','btn_edit_xianshi_goods_submit');
                        } else {
                            layer.msg(data.message);
                        }
                    },
                    'json');
        });

        //添加限时折扣商品
        $('body').on('click', '#btn_add_xianshi_goods_submit', function () {
            //先生成模版
            var goods_id = $('#dialog_goods_id').val();
            var xianshi_id = {$re['xianshi_id']};
            var goods_price = Number($('#dialog_input_goods_price').val());
            var xianshi_price = Number($('#dialog_xianshi_price').val());
            if (!isNaN(xianshi_price) && xianshi_price > 0 && xianshi_price < goods_price) {
                $.post("{:U('getGoods')}",
                        {goods_id: goods_id, xianshi_id: xianshi_id, xianshi_price: xianshi_price},
                        function (data) {
                            if (true) {
                                $('#dialog_add_xianshi_goods').hide();
                                $('#xianshi_goods_list').prepend(template.render('xianshi_goods_list_template', data.xianshi_goods)).hide().fadeIn('slow');
                                $('#xianshi_goods_list_norecord').hide();
                                $(".dialog-win").remove();
                                $(".dialog-mask").remove();
                                temp = '<div id="mydialog">' + temp + '</div>';
                                $("body").prepend(temp);
                                layer.msg(data.message);
                            } else {
                                layer.msg(data.message);
                            }
                        },
                        'json');
            } else {
                $('#dialog_add_xianshi_goods_error').show();
            }
        });


        //编辑限时活动商品
        $('body').on('click','#btn_edit_xianshi_goods_submit', function () {
            var xianshi_goods_id = $('#dialog_goods_id').val();
            var xianshi_price = Number($('#dialog_xianshi_price').val());
            var goods_price = Number($('#dialog_input_goods_price').val());
            
            if (!isNaN(xianshi_price) && xianshi_price > 0 && xianshi_price < goods_price) {
                $.post("{:U('getEditGoods')}",
                        {xianshi_goods_id: xianshi_goods_id, xianshi_price: xianshi_price, goods_price: goods_price},
                        function (data) {
                            if (data.result) {
                                $edit_item.find('[nctype="xianshi_price"]').text(data.xianshi_price);
                                $edit_item.find('[nctype="xianshi_discount"]').text(data.xianshi_discount);
                                $(".dialog-win").remove();
                                $(".dialog-mask").remove();
                                $('#dialog_edit_xianshi_goods').hide();
                                temp = '<div id="mydialog">' + temp + '</div>';
                                $("body").prepend(temp);
                                layer.msg(data.message, {icon: 1});
                            } else {
                                layer.msg(data.message, {icon: 2});
                            }
                        }, 'json'
                        );
            } else {
                $('#dialog_add_xianshi_goods_error').show();
            }
        });

        //删除限时活动商品
        $('#xianshi_goods_list').on('click', '[nctype="btn_del_xianshi_goods"]', function () {
            var $this = $(this);
            var xianshi_goods_id = $(this).attr('data-xianshi-goods-id');
            //询问框
            layer.confirm('您确认取消此商品的限时折扣吗？', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                $.post("{:U('deleteGoods')}",
                        {xianshi_goods_id: xianshi_goods_id},
                        function (data) {
                            if (data.result) {
                                $this.parents('tr').hide('slow', function () {
                                    var xianshi_goods_count = $('#xianshi_goods_list').find('.bd-line:visible').length;
                                    if (xianshi_goods_count <= 0) {
                                        $('#xianshi_goods_list_norecord').show();
                                    }
                                });
                                layer.msg(data.message, {icon: 1});
                            } else {
                                layer.msg(data.message, {icon: 2});
                            }
                        }, 'json'
                        );
            }, function () {});
        });

        //渲染限时折扣商品列表
        xianshi_goods_array = $.parseJSON('{$arrs}');
        if (xianshi_goods_array.length > 0) {
            var xianshi_goods_list = '';
            $.each(xianshi_goods_array, function (index, xianshi_goods) {
                xianshi_goods_list += template.render('xianshi_goods_list_template', xianshi_goods);
            });
            $('#xianshi_goods_list').prepend(xianshi_goods_list);
        } else {
            $('#xianshi_goods_list_norecord').show();
        }
    }
    );
</script>

<script id="xianshi_goods_list_template" type="text/html">
    <tr class="bd-line">
        <td></td>
        <td><div class="pic-thumb"><a href="<%=goods_url%>" target="_blank"><img src="<%=goods_image%>!100x100" width='50' height='50' alt=""></a></div></td>
        <td class="tl"><dl class="goods-name"><dt><a href="<%=goods_url%>" target="_blank"><%=goods_name%></a></dt></dl></td>
        <td>&yen;<%=goods_price%></td>
        <td>&yen;<span nctype="xianshi_price"><%=xianshi_price%></span></td>
        <td><span nctype="xianshi_discount"><%=xianshi_discount%></span></td>
        <td class="nscs-table-handle">
            <a nctype="btn_edit_xianshi_goods" class="button bg-blue button-little dialogs" data-toggle="click" id="myGoods" data-target="#mydialog" data-mask="1" data-width="20%" data-xianshi-goods-id="<%=xianshi_goods_id%>" data-goods-price="<%=goods_price%>"><i class="icon-edit"></i>编辑</a>
            <a nctype="btn_del_xianshi_goods" class="button bg-blue button-little" data-xianshi-goods-id="<%=xianshi_goods_id%>" href="javascript:void(0);"><i class="minus-circle"></i>删除</a>
        </td>
    </tr>
</script>
